﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Lv1
{
    /// <summary>
    /// 全局音效单例对象
    /// </summary>
    public class SoundManager : DDOLSingleton<SoundManager>
    {
        private int _musicVolume = 100;
        private int _soundVolume = 100;

        public bool StopSound { get; set; }
        public bool StopMusic { get; set; }

        public int MuiscVolume {
            get { return _musicVolume; }
            set
            {
                _musicVolume = value;
                _MusicAudioSource.volume = _musicVolume/100f;

            }
        }

        public int SoundVolume
        {
            get
            {
                return _soundVolume;
            }
            set
            {
                _soundVolume = value;
                _AddtionAudioSources.ForEach(audioSouce => audioSouce.volume = _musicVolume/100f);


            }
        }


        //private Dictionary<GameObject, GameObject> dicAddtionAudioSource = new Dictionary<GameObject, GameObject>();

        private List<AudioSource> _AddtionAudioSources = new List<AudioSource>();
        private AudioSource _MusicAudioSource;

        void Awake()
        {
            _MusicAudioSource = gameObject.GetOrAddComponent<AudioSource>();
        }


        /// <summary>
        /// 在目标对象获取或者增加一个Audiosource
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public AudioSource GetAudioSource(GameObject target)
        {
            AudioSource audioSource = target.GetOrAddComponent<AudioSource>();
            audioSource.volume = SoundVolume/100f;
            audioSource.enabled = !StopSound;
            return audioSource;
        }

        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="path">路径</param>
        public void PlayMusic(string path)
        {
            AudioClip audioClip = Resources.Load<AudioClip>(path);
            PlayMusic(audioClip);

        }
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="audioClip">音乐片段</param>
        public void PlayMusic(AudioClip audioClip)
        {
            if (StopMusic)
            {
                return;
            }

            _MusicAudioSource.loop = true;
            _MusicAudioSource.loop = true;
            _MusicAudioSource.clip = audioClip;
            _MusicAudioSource.Play();
        }
        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="path">路径</param>
        public void PlaySound(string path)
        {
            AudioClip audioClip = Resources.Load<AudioClip>(path);
            if (audioClip != null)
                PlaySound(audioClip);
        }
        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="audioClip">音乐片段</param>
        public void PlaySound(AudioClip audioClip)
        {
            if (StopSound)
            {
                return;
            }
            if (audioClip == null)
                return;
            GameObject go = new GameObject(audioClip.name + "Sound");
            go.transform.SetParent(transform);
            StartCoroutine(PlaySound(go, audioClip));
        }

        private IEnumerator PlaySound(GameObject go, AudioClip audioClip)
        {
            if (audioClip == null)
                yield break;
            AudioSource audioSource = go.GetOrAddComponent<AudioSource>();
            audioSource.PlayOneShot(audioClip);
            audioSource.volume = SoundVolume/100f;
            yield return new WaitForSeconds(audioClip.length);
            Destroy(go);
        }

        /// <summary>
        /// 暂停所有声音
        /// </summary>
        public void Pause()
        {
            AudioListener.pause = true;
        }

        /// <summary>
        /// 恢复播放声音
        /// </summary>
        public void Resume()
        {
            AudioListener.pause = false;
        }
        /// <summary>
        /// 暂停背景音乐
        /// </summary>
        public void PauseMusic()
        {
            gameObject.GetComponent<AudioSource>().Pause();
            StopMusic = true;
        }

        /// <summary>
        /// 恢复背景音乐
        /// </summary>
        public void ResumeMusic()
        {
            gameObject.GetComponent<AudioSource>().UnPause();
            StopMusic = false;
        }


        public void PauseSound()
        {
            this.StopSound = true;
            foreach (var audioSource in _AddtionAudioSources)
            {
                audioSource.GetComponent<AudioSource>().enabled = false;
            }
        }

        public void ResumeSound()
        {
            this.StopSound = false;
            foreach (var audioSource in _AddtionAudioSources)
            {
                audioSource.GetComponent<AudioSource>().enabled = true;
            }
        }



        private IEnumerator IPauseMusicWhenPlaySound(string path)
        {
            PauseMusic();
            AudioClip clip = Resources.Load<AudioClip>(path);
            PlaySound(clip);
            yield return new WaitForSeconds(clip.length);
            ResumeMusic();
        }

        /// <summary>
        /// 播放音效是暂停背景音乐
        /// </summary>
        /// <param name="path"></param>
        public void PauseMusicWhenPlaySound(string path)
        {
            StartCoroutine(IPauseMusicWhenPlaySound(path));
        }
    }
}
